package com.liaowei.novel.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.liaowei.novel.R;
import com.liaowei.novel.domain.Novel;
import com.liaowei.novel.domain.vo.NovelVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NovelListActivity extends AppCompatActivity {

    private SQLiteDatabase db;
    private ListView novel_list_lv;
    private String[] from = {"novelId","novelCoverImage", "novelName", "novelAuthor", "novelDescription"};
    private int[] to = {R.id.novel_id,R.id.novel_cover_image, R.id.novel_name, R.id.novel_author, R.id.novel_description};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.novel_list);
        this.db = SQLiteDatabase.openOrCreateDatabase("/data/data/com.liaowei.novel/databases/novel.db", null);
        Intent intent = getIntent();
        String cateName = intent.getStringExtra("cateName");
        setTitle(cateName);
        init();
        // 渲染listView
        SimpleAdapter adapter = new SimpleAdapter(this, getNovelData(cateName), R.layout.novel_item, this.from, this.to);
        novel_list_lv.setAdapter(adapter);

    }

    // 获取小说列表
    public List<Map<String, Object>> getNovelData(String cateName){
        List<Map<String, Object>> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select n.id,n.name,n.coverImage,n.description,u.nickname as author\n" +
                "from novel n\n" +
                "join user u on u.id = n.user_id\n" +
                "join category c on c.id = n.category_id\n" +
                "where c.name = '" + cateName + "'", null);
        while (cursor.moveToNext()){
            Map<String, Object> map = new HashMap<>();
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String coverImage = cursor.getString(cursor.getColumnIndex("coverImage"));
            String description = cursor.getString(cursor.getColumnIndex("description"));
            String author = cursor.getString(cursor.getColumnIndex("author"));
            map.put("novelId",id);
            map.put("novelCoverImage",coverImage);
            map.put("novelName",name);
            map.put("novelAuthor",author);
            map.put("novelDescription",description);
            list.add(map);
        }

        return list;
    }

    class SimpleAdapter extends android.widget.SimpleAdapter {
        public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            Map<String, Object> item = (Map<String, Object>) getItem(position);
            String coverImage = (String) item.get("novelCoverImage");
            ImageView iv = v.findViewById(R.id.novel_cover_image);
            Glide.with(v).load(coverImage).into(iv);

            LinearLayout line = v.findViewById(R.id.novel_item);
            line.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TextView writeId = v.findViewById(R.id.novel_id);
                    String strId = writeId.getText().toString();
                    int id = Integer.parseInt(strId);
                    Intent intent = new Intent(NovelListActivity.this, NovelDetailActivity.class);
                    intent.putExtra("id",id);
                    startActivity(intent);
                }
            });

            return v;
        }
    }



    // 初始化组件
    public void init(){
        this.novel_list_lv = findViewById(R.id.novel_list_lv);
    }
}